<template>
    <div>
        <el-page-header @back="gotoback" title="返回">
            <template #content>
                <el-breadcrumb-item :to="{ path: '/practice' }">任务·列表</el-breadcrumb-item>
            </template>
        </el-page-header>
    </div>
    <el-skeleton :rows="8" :loading="load" animated>
        <div class="header">
            <div class="head">
                <el-row>
                    <el-col :span="8">

                        <el-button type="primary" style="margin-top: 1vw;margin-right: 20vw;"
                            @click="taskadd">新增</el-button>

                    </el-col>
                    <el-col :span="8">
                        <el-input placeholder="输入姓名或账号进行查询" v-model="select" @input="btnselect" style="margin-top: 1vw;">
                            <template #append>
                                <el-button>查询</el-button>
                            </template>
                        </el-input>
                    </el-col>
                    <el-col :span="8">
                        <div style="margin-top: 2vh;">
                            <el-checkbox @change="(val) => checkbox(val, item)" v-for="(item, index) in detailsList"
                                :label="item.title" :key="index">
                                {{ item.title }}
                            </el-checkbox>
                        </div>
                    </el-col>
                </el-row>
            </div>

            <div class="main">
                <el-table v-loading="loading" :data="tableData" border class="table" max-height="55vh">
                    <el-table-column fixed prop="id" label="序号" type="index" width="80">
                        <template #default="score">
                            {{ (obj.page - 1) * obj.limit + score.$index + 1 }}
                        </template>
                    </el-table-column>
                    <el-table-column type="expand">
                        <template #default="scope">
                            <div :key="obj.tid" v-for="obj in scope.row.details">
                                <el-tag type="primary" v-if="obj.isFixed == 1">{{ obj.name }}</el-tag>
                                <el-tag type="success" v-else>{{ obj.name }}</el-tag> &nbsp;
                                <el-tag type="primary" v-if="obj.isFixed == 1">模式:固定题目</el-tag>
                                <el-tag type="success" v-else>模式:随机题目</el-tag> &nbsp;
                                <el-tag type="primary" v-if="obj.isFixed == 1">正确率:{{ obj.rightPercent }}%</el-tag>
                                <el-tag type="success" v-else>正确率:{{ obj.rightPercent }}%</el-tag> &nbsp;
                                <el-tag type="primary" v-if="obj.isFixed == 1">题目数量:{{ obj.limitCount }}</el-tag>
                                <el-tag type="success" v-else>题目数量:{{ obj.limitCount }}</el-tag> &nbsp;
                                <el-tag type="primary" v-if="obj.isFixed == 1">难度区间:{{ obj.minLv }}~{{ obj.maxLv }}</el-tag>
                                <el-tag type="success" v-else>难度区间:{{ obj.minLv }}~{{ obj.maxLv }}</el-tag>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="name" label="任务" width="130">
                    </el-table-column>
                    <el-table-column prop="lv" label="难度" width="110">
                    </el-table-column>
                    <el-table-column prop="isPublish" label="发布" width="110">
                        <template #default="scope">
                            <el-tag :type="scope.row.isPublish == 1 ? 'success' : 'danger'">
                                {{ scope.row.isPublish == 1 ? '是' : '是' }}
                            </el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column prop="isBan" label="禁用" width="110">
                        <template #default="scope">
                            <el-tag :type="scope.row.isBan == 0 ? 'success' : 'danger'">
                                {{ scope.row.isBan == 0 ? '否' : '是' }}
                            </el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column prop="labels" label="标签" width="130">
                    </el-table-column>
                    <el-table-column prop="useCount" label="使用" width="100">
                    </el-table-column>
                    <el-table-column prop="time" label="时间">
                    </el-table-column>
                    <el-table-column label="操作" fixed="right" width="200">
                        <template #default="scope">
                            <el-button size="small" type="primary" @click="detail(scope.row)">详情</el-button>
                            <el-button size="small" type="warning" v-if="scope.row.isPublish == 1 && scope.row.isBan == 0"
                                @click="disable(scope.row)">禁用</el-button>
                            <el-button size="small" type="success" v-else
                                v-show="scope.row.isPublish == 0 && scope.row.isBan == 0"
                                @click="blish(scope.row)">发布</el-button>
                            <el-button size="small" type="info" v-if="scope.row.isPublish == 1 && scope.row.isBan == 0"
                                @click="Cancel(scope.row)">取消</el-button>
                            <el-button size="small" type="danger" v-else
                                v-show="scope.row.isPublish == 0 && scope.row.isBan == 0" plain
                                @click="deteles(scope.row)">删除</el-button>
                            <el-button size="small" type="success" v-show="scope.row.isPublish == 1 && scope.row.isBan == 1"
                                plain @click="taskma(scope.row)">启用</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
        </div>
        <div class="example-pagination-block" style="margin-top: 1vh;">
            <el-pagination @current-change="changes" :page-size="obj.limit" :current-page="obj.page"
                layout="prev, pager, next" :total="total" />
        </div>
    </el-skeleton>
</template>
<script lang="ts">
import { defineComponent, reactive, onMounted, toRefs } from 'vue'
import { GetTasks, BanTasks, PublishTasks, DeleteTasks } from '../../../api/practice/task/index'
import { useRouter } from 'vue-router';
import { ElMessageBox } from 'element-plus';
import { ElMessage } from 'element-plus'

export default defineComponent({
    setup() {
        const router = useRouter();

        const data = reactive({
            load: true, //骨架屏
            select: '',//查询
            tableData: [],//数据
            loading: true,//加载

            total: 0,//总条数
            details: [],
            key: '',
            obj: {
                page: 1,//当前页
                limit: 10,//每页显示多少条
                index: 0,
                isBan: -1,
                isPublish: -1,
            },
            detailsList: [
                {
                    "title": "禁用",
                },
                {
                    "title": "启用",
                },
                {
                    "title": "发布",
                },
                {
                    "title": "未发布",
                }
            ],
        })

        const events = {
            data() {
                GetTasks({
                    page: data.obj.page,
                    limit: data.obj.limit,
                    key: data.select
                }).then((res: any) => {
                    console.log("任务数据---=", res)
                    data.tableData = res.data.data
                    data.loading = false
                    data.load = false
                    data.total = res.data.total
                })
            },
            //返回上一级
            gotoback() {
                window.history.go(-1)
            },
            //新增
            taskadd(val: any) {
                console.log("新增id", val.id)
                router.push('task/add')
            },
            //详情
            detail(val: any) {
                console.log("详情id", val.id)
                router.push({ path: `task/detail/${val.id}` })
            },
            //监听查询
            btnselect() {
                data.loading = true
                events.data()
            },
            //点击page触发事件
            changes(val: any) {
                data.loading = true
                data.obj.page = val
                events.data()
            },
            //删除
            deteles(obj: any) {
                ElMessageBox.confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    data.loading = true
                    DeleteTasks({
                        ids: [obj.id]
                    }).then((res: any) => {
                        if (res.result === 0) {
                            ElMessage({
                                message: '删除成功',
                                type: 'success',
                            })
                            events.data()
                        } else {
                            ElMessage.error({
                                message: '删除失败',
                                type: 'error',
                            })
                        }
                        data.loading = false
                    })
                }).catch(() => {
                    ElMessage.info("已取消删除")
                })


            },
            //禁用
            disable(val: any) {
                data.loading = true
                BanTasks({
                    ids: [val.id],
                    isBan: 1
                }).then((res: any) => {
                    if (res.result == 0) {
                        ElMessage({
                            message: res.message,
                            type: 'success',
                        })
                        events.data()
                    } else {
                        ElMessage({
                            message: res.message,
                            type: 'error',
                        })
                    }
                })
            },
            //启用
            taskma(val: any) {
                data.loading = true
                BanTasks({
                    ids: [val.id],
                    isBan: 0
                }).then((res: any) => {
                    if (res.result == 0) {
                        ElMessage({
                            message: res.message,
                            type: 'success',
                        })
                        events.data()
                    } else {
                        ElMessage({
                            message: res.message,
                            type: 'error',
                        })
                    }
                })
            },
            //发布
            blish(val: any) {
                data.loading = true
                PublishTasks({
                    ids: [val.id],
                    isPublish: 1
                }).then((res: any) => {
                    if (res.result == 0) {
                        ElMessage({
                            message: res.message,
                            type: 'success',
                        })
                        events.data()
                    } else {
                        ElMessage({
                            message: res.message,
                            type: 'error',
                        })
                        events.data()
                    }
                })
            },
            //取消
            Cancel(val: any) {
                data.loading = true
                PublishTasks({
                    ids: [val.id],
                    isPublish: 0
                }).then((res: any) => {
                    if (res.result == 0) {
                        ElMessage({
                            message: res.message,
                            type: 'success',
                        })
                        events.data()
                    } else {
                        ElMessage({
                            message: res.message,
                            type: 'error',
                        })
                        events.data()
                    }
                })
            },
            checkbox(val: any, name: any) {
                console.log(name.title)
                if (name.title == "启用") {
                    data.loading = true
                    if (val) {
                        if (data.obj.isBan != -1) {
                            data.obj.isBan = -1
                            events.data()
                            return
                        }
                        data.obj.isBan = 0
                        events.data()
                    } else {
                        if (data.obj.isBan == -1) {
                            data.obj.isBan = 1
                            events.data()
                            return
                        }
                        data.obj.isBan = -1
                        events.data()
                    }
                }
                if (name.title == "禁用") {
                    data.loading = true
                    if (val) {
                        if (data.obj.isBan != -1) {
                            data.obj.isBan = -1
                            events.data()
                            return
                        }
                        data.obj.isBan = 1
                        events.data()
                    } else {
                        if (data.obj.isBan == -1) {
                            data.obj.isBan = 0
                            events.data()
                            return
                        }
                        data.obj.isBan = -1
                        events.data()
                    }
                }
                if (name.title == "发布") {
                    data.loading = true
                    if (val) {
                        if (data.obj.isPublish != -1) {
                            data.obj.isPublish = -1
                            events.data()
                            return
                        }
                        data.obj.isPublish = 1
                        events.data()
                    } else {
                        if (data.obj.isPublish == -1) {
                            data.obj.isPublish = 0
                            events.data()
                            return
                        }
                        data.obj.isPublish = -1
                        events.data()
                    }
                }
                if (name.title == "未发布") {
                    data.loading = true
                    if (val) {
                        if (data.obj.isPublish != -1) {
                            data.obj.isPublish = -1
                            events.data()
                            return
                        }
                        data.obj.isPublish = 0
                        events.data()
                    } else {
                        if (data.obj.isPublish == -1) {
                            data.obj.isPublish = 1
                            events.data()
                            return
                        }
                        data.obj.isPublish = -1
                        events.data()
                    }
                }
            },
        }

        onMounted(() => {
            events.data()
        })

        return {
            ...toRefs(data),
            ...events,
        }
    },
})
</script>
<style scoped>
.head {
    border: #ada5a5 solid 2px;
    height: 60px;
    box-shadow: 3px 1px 1px 1px #ccc;
    border-radius: 5px;
    margin-top: 3vh;
    width: 96%;
    margin-left: 3vh;
}

.main {
    margin-top: 3vh;
    box-shadow: 3px 1px 1px 1px #ccc;
    margin-left: 3vh;
}

.table {
    width: 98%;
    border-radius: 10px;
    border: #ada5a5 solid 2px;
}

.header {
    border: #ccc solid 1px;
    margin-top: 2vh;
    background-color: rgba(239, 240, 245, 0.932);
    border-radius: 5px;
}
</style>
